//
//  BGCommitViewController.m
//  bigMall
//
//  Created by LTOVE on 16/9/14.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGCommitViewController.h"
#import "LTOVEDynamicCreakButton.h"
@interface BGCommitViewController ()<UITextViewDelegate,dynamicButtonDelegate>{
    
//    NSString *fitstValue;
//    NSString *secValue;
//    NSString *threeValue;
    int num;
    NSMutableArray *btnsArray;
    UIGestureRecognizer *gust;
}
@property (nonatomic,copy)NSString *fitstValue;
@property (nonatomic,copy)NSString *secValue;
@property (nonatomic,copy)NSString *threeValue;
@property (nonatomic,copy)NSString *titles;
@end

@implementation BGCommitViewController

//- (BOOL)writeEnabel
//{
//    if (!_writeEnabel) {
//        _writeEnabel = YES;
//    }
//    return _writeEnabel;
//}
- (void)dynamicButtonClick:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:NO];
    if ([_delegate respondsToSelector:@selector(commitViewBtnClickWithContent:scoreOne:scoreTwo:scoreThree:)]) {
        [_delegate commitViewBtnClickWithContent:_titles scoreOne:self.fitstValue scoreTwo:self.secValue scoreThree:self.threeValue];
    }
}
-(void)setWriteEnabel:(BOOL)writeEnabel
{
    _writeEnabel = writeEnabel;
    
    if (writeEnabel) {
        [self setUpAllSubviews];
    }
}
+ (instancetype)initControlWithCommitText:(NSString *)commitText anddsorceValueOne:(NSString *)sorceOne sorceTwo:(NSString *)scoreTwo scoreThree:(NSString *)scoreThree
{
    BGCommitViewController *vc = [[self alloc]init];
    vc.writeEnabel = NO;
    vc.fitstValue = sorceOne;
    vc.secValue = scoreTwo;
    vc.threeValue = scoreThree;
    vc.titles = commitText;
    [vc setUpAllSubviews];
    return vc;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
    
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
    GWLog(@"sss");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = dockwhite_;
    
    
//    if (self.writeEnabel) {
//        [self setUpAllSubviews];
//    }
//    [self setUpAllSubviews];
    // Do any additional setup after loading the view.
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
//    self.title = textView.text;
    GWLog(@"%@",self.title);
}

- (void)setUpAllSubviews
{
    //第一个视图
    UIView *viewF = [[UIView alloc]initWithFrame:CGRectMake(0, 74, screenW, 100)];
    viewF.backgroundColor = lightwhite_;
    UILabel *label = [UILabel new];
    label.text = @"填写评论:";
    label.x = BGMaginLeft;
    label.y = BGMaginTop;
    [label sizeToFit];
    
    UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(label.frame), label.y -5, screenW - label.width - 2 * BGMaginLeft, viewF.height - 2 * BGMaginTop)];
//    textView.backgroundColor = read_;
    textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    textView.delegate = self;
    textView.font = Font30;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    if (!self.writeEnabel) {
        textView.text = self.titles;
        textView.userInteractionEnabled = NO;
    }
    
    [viewF addSubview:textView];
    
    [viewF addSubview:label];
    //第二个视图
    UIView *viewT = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(viewF.frame) + 10, screenW, 230)];
    viewT.backgroundColor = lightwhite_;
    UILabel *labels = [UILabel new];
    labels.text = @"进行评分";
    labels.x = BGMaginLeft;
    labels.y = BGMaginTop;
    [labels sizeToFit];
    [viewT addSubview:labels];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(BGMaginLeft, CGRectGetMaxY(labels.frame) + BGMaginTop, screenW - 2 * BGMaginLeft, 1)];
    lineView.backgroundColor = lightGray_;
    
    UIView *ValueViewF = [self addStartWithTitle:@"服务态度"];
    ValueViewF.y = CGRectGetMaxY(lineView.frame);
    [viewT addSubview:ValueViewF];
    UIView *valueViewT = [self addStartWithTitle:@"货物品质"];
    valueViewT.y = CGRectGetMaxY(ValueViewF.frame);
    [viewT addSubview:valueViewT];
    UIView *valueViewThre = [self addStartWithTitle:@"发货效率"];
    valueViewThre.y = CGRectGetMaxY(valueViewT.frame);
    [viewT addSubview:valueViewThre  ];
    [viewT addSubview:lineView];
    [self.view addSubview:viewF];
    [self.view addSubview:viewT];
    if (self.writeEnabel) {
        NSDictionary *dic = @{
                              NSForegroundColorAttributeName:lightwhite_
                              };
        LTOVEDynamicCreakButton *view = [LTOVEDynamicCreakButton buttonViewWithFrame:CGRectMake(0, screenH - 70, screenW, 70) titleDataArray:@[@"发表评论"] andEdgInset:UIEdgeInsetsMake(5, 20, 5, 20) andMaginX:0 andMaginY:0 andBtnHeigh:50 andBtnNorColor:read_ andBtnHeighColor:read_ andRadius:5 andFont:dic];
        view.backgroundColor = [UIColor clearColor];
        view.delegate = self;
        [self.view addSubview:view];
    }
}

- (UIView *)addStartWithTitle:(NSString *)title
{
    if ([title isEqualToString:@"服务态度"]) {
        num = 0;
    }else if ([title isEqualToString:@"货物品质"]){
        num = 1;
    }else
    {
        num = 2;
    }
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenW, 45)];
    
    UILabel *titles = [UILabel new];
    titles.text = title;
    titles.x = BGMaginLeft;
    titles.centerY = view.height * 0.5;
    [titles sizeToFit];
    [view addSubview:titles];
    NSMutableArray *btnArray = [NSMutableArray array];
    for (int i = 0; i < 5; i ++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.width = btn.height = 20;
        [btn setBackgroundImage:[UIImage imageNamed:@"形状-2"] forState:UIControlStateSelected];
        [btn setBackgroundImage:[UIImage imageNamed:@"形状-3"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"形状-3"] forState:UIControlStateHighlighted];
        [btn setAdjustsImageWhenHighlighted:YES];
        btn.tag = i;
        [btn setTitle:title forState:UIControlStateNormal];
        btn.font = [UIFont systemFontOfSize:0];
        btn.centerY = titles.centerY;
        btn.x = self.view.centerX  + (btn.width + 5) * i  ;
        [btn addTarget:self action:@selector(btnClickWith:) forControlEvents:UIControlEventTouchDown];
        if (!self.writeEnabel) {
            switch (num) {
                case 0:
                {
                    int valus = [self.fitstValue intValue];
                    btn.userInteractionEnabled = NO;
                    if (i<= valus-1) {
                        btn.selected = YES;
                        
                    }
                }
                    
                    break;
                case 1:
                    
                {
                    int valus = [self.secValue intValue];
                    btn.userInteractionEnabled = NO;
                    if (i<= valus-1) {
                        btn.selected = YES;
//                        btn.userInteractionEnabled = NO;
                    }
                }
                    break;
                case 2:
                    
                {
                    int valus = [self.threeValue intValue];
                    btn.userInteractionEnabled = NO;
                    if (i<= valus-1) {
                        btn.selected = YES;
//                        btn.userInteractionEnabled = NO;
                    }
                }
                    break;
                    
                default:
                    break;
            }
        }
        [btnArray addObject:btn];
        [view addSubview:btn];
        
    }
    if (!btnsArray) {
        btnsArray = [NSMutableArray array];
    }
    [btnsArray addObject:btnArray];
    return view;
}

- (void)btnClickWith:(UIButton *)btn
{
    if ([btn.titleLabel.text isEqualToString:@"服务态度"]) {
        num = 0;
    }else if ([btn.titleLabel.text isEqualToString:@"货物品质"]){
        num = 1;
    }else
    {
        num = 2;
    }

    GWLog(@"第%d组%ld分%@----%d",num,btn.tag,btn.titleLabel.text,num);
//    btn.selected = !btn.selected;
    switch (num) {
        case 0:
            _fitstValue = [NSString stringWithFormat:@"%ld",btn.tag + 1];
        {
            NSArray *array = btnsArray[0];
            for (int i = 0; i < array.count; i++) {
                UIButton *btns = array[i];
                GWLog(@"%d---%ld",i,(long)btn.tag);
                if (i<= btn.tag) {
                    btns.selected = YES;
                }else{
                    btns.selected = NO;
                }
            }
        }
            break;
        case 1:
            _secValue = [NSString stringWithFormat:@"%ld",btn.tag + 1];
        {
            NSArray *array = btnsArray[1];
            for (int i = 0; i < array.count; i++) {
                UIButton *btns = array[i];
                if (i<=btn.tag) {
                    btns.selected = YES;
                }else{
                    btns.selected = NO;
                }
            }
        }
            break;
        case 2:
            _threeValue = [NSString stringWithFormat:@"%ld",btn.tag + 1];
        {
            NSArray *array = btnsArray[2];
            for (int i = 0; i < array.count; i++) {
                UIButton *btns = array[i];
                if (i<=btn.tag) {
                    btns.selected = YES;
                }else{
                    btns.selected = NO;
                }
            }
        }
            break;
        default:
            break;
    }
}



@end
